//
//  AFHTTPSessionManager+KLRACSupport.m
//  KoalaUtilKit
//
//  Created by Tommy on 15/1/10.
//  Copyright (c) 2015年 Koala Team. All rights reserved.
//

#import "AFHTTPSessionManager+KLRACSupport.h"

@implementation AFHTTPSessionManager (KLRACSupport)
- (RACSignal *)klrac_GET:(NSString *)path parameters:(id)parameters {
    return [[self klrac_requestPath:path parameters:parameters method:@"GET"]
            setNameWithFormat:@"%@ -klrac_GET: %@, parameters: %@", self.class, path, parameters];
}

- (RACSignal *)klrac_HEAD:(NSString *)path parameters:(id)parameters {
    return [[self klrac_requestPath:path parameters:parameters method:@"HEAD"]
            setNameWithFormat:@"%@ -klrac_HEAD: %@, parameters: %@", self.class, path, parameters];
}

- (RACSignal *)klrac_POST:(NSString *)path parameters:(id)parameters {
    return [[self klrac_requestPath:path parameters:parameters method:@"POST"]
            setNameWithFormat:@"%@ -klrac_POST: %@, parameters: %@", self.class, path, parameters];
}

- (RACSignal *)klrac_POST:(NSString *)path parameters:(id)parameters constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block {
    return [[RACSignal createSignal:^(id<RACSubscriber> subscriber) {
        NSMutableURLRequest *request = [self.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:[[NSURL URLWithString:path relativeToURL:self.baseURL] absoluteString] parameters:parameters constructingBodyWithBlock:block error:nil];
        
        NSURLSessionDataTask *task = [self dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
            if (error) {
                [subscriber sendError:error];
            } else {
                [subscriber sendNext:responseObject];
                [subscriber sendCompleted];
            }
        }];
        
        [task resume];
        
        return [RACDisposable disposableWithBlock:^{
            [task cancel];
        }];
    }] setNameWithFormat:@"%@ -klrac_POST: %@, parameters: %@, constructingBodyWithBlock:", self.class, path, parameters];
    ;
}

- (RACSignal *)klrac_PUT:(NSString *)path parameters:(id)parameters {
    return [[self klrac_requestPath:path parameters:parameters method:@"PUT"]
            setNameWithFormat:@"%@ -klrac_PUT: %@, parameters: %@", self.class, path, parameters];
}

- (RACSignal *)klrac_PATCH:(NSString *)path parameters:(id)parameters {
    return [[self klrac_requestPath:path parameters:parameters method:@"PATCH"]
            setNameWithFormat:@"%@ -klrac_PATCH: %@, parameters: %@", self.class, path, parameters];
}

- (RACSignal *)klrac_DELETE:(NSString *)path parameters:(id)parameters {
    return [[self klrac_requestPath:path parameters:parameters method:@"DELETE"]
            setNameWithFormat:@"%@ -klrac_DELETE: %@, parameters: %@", self.class, path, parameters];
}

- (RACSignal *)klrac_requestPath:(NSString *)path parameters:(id)parameters method:(NSString *)method {
    return [RACSignal createSignal:^(id<RACSubscriber> subscriber) {
        NSURLRequest *request = [self.requestSerializer requestWithMethod:method URLString:[[NSURL URLWithString:path relativeToURL:self.baseURL] absoluteString] parameters:parameters error:nil];
        

        
        NSURLSessionDataTask *task = [self dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
            if (error) {
                [subscriber sendError:error];
            } else {
                [subscriber sendNext:responseObject];
                [subscriber sendCompleted];
            }
        }];
        [task resume];
        
        return [RACDisposable disposableWithBlock:^{
            [task cancel];
        }];
    }];
}

@end
